package com.cold.legendary.bprocess.db.repository;

import com.cold.legendary.bprocess.db.mapper.TbProcessRequestLogMapper;
import com.cold.legendary.bprocess.db.model.TbProcessRequest;
import com.cold.legendary.bprocess.db.model.TbProcessRequestLogWithBLOBs;
import com.cold.legendary.bprocess.db.repository.dto.ReqLogInitDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author cold
 * @since 2023/3/10
 */
@Slf4j
@Service
public class ProcessRequestLogRepository {

    @Resource
    private TbProcessRequestLogMapper tbProcessRequestLogMapper;


    /**
     * 初始化流程请求记录
     *
     * @param reqLogInitDto 初始化请求日志入参
     * @return 主键id
     */
    public Long initTbProcessRequestLog(ReqLogInitDto reqLogInitDto) {

        TbProcessRequest qryProcessRequest = reqLogInitDto.getQryProcessRequest();
        String oldPointRunStatus = reqLogInitDto.getOldPointRunStatus();
        String newPointRunStatus = reqLogInitDto.getNewPointRunStatus();
        String oldPointId = reqLogInitDto.getOldPointId();
        String newPointId = reqLogInitDto.getNewPointId();
        String oldPointName = reqLogInitDto.getOldPointName();
        String newPointName = reqLogInitDto.getNewPointName();
        String oldBContext = reqLogInitDto.getOldBContext();
        String newBContext = reqLogInitDto.getNewBContext();

        String remark = reqLogInitDto.getRemark();

        TbProcessRequestLogWithBLOBs initProcessRequestLog = new TbProcessRequestLogWithBLOBs();
        initProcessRequestLog.setRequestId(qryProcessRequest.getRequestId());
        initProcessRequestLog.setBizId(qryProcessRequest.getBizId());
        initProcessRequestLog.setBizRefId(qryProcessRequest.getBizRefId());
        initProcessRequestLog.setMerchantId(qryProcessRequest.getMerchantId());
        initProcessRequestLog.setProcessId(qryProcessRequest.getProcessId());
        initProcessRequestLog.setProcessName(qryProcessRequest.getProcessName());
        initProcessRequestLog.setBeforePointRunStatus(oldPointRunStatus);
        initProcessRequestLog.setAfterPointRunStatus(newPointRunStatus);
        initProcessRequestLog.setBeforePointId(oldPointId);
        initProcessRequestLog.setBeforePointName(oldPointName);
        initProcessRequestLog.setAfterPointId(newPointId);
        initProcessRequestLog.setAfterPointName(newPointName);
        initProcessRequestLog.setCreateTime(new Date());
        initProcessRequestLog.setUpdateTime(new Date());
        initProcessRequestLog.setIsDeleted("N");
        initProcessRequestLog.setBeforeRunContext(oldBContext);
        initProcessRequestLog.setAfterRunContext(newBContext);
        initProcessRequestLog.setRemark(remark);

        int result = tbProcessRequestLogMapper.insertSelective(initProcessRequestLog);
        Assert.isTrue(result > 0, "数据库操作失败");
        return initProcessRequestLog.getId();
    }
}
